package org.springcloud.wisdom.spring_01_canteen.address;

import org.springcloud.wisdom.spring_01_canteen.common.bean.ResponseData;
import org.springcloud.wisdom.spring_01_canteen.common.controller.BaseController;
import org.springcloud.wisdom.spring_01_canteen.dishes.fordishes.Dishes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController extends BaseController {
    @Autowired
    private AddressService addressService;
    //查看地址
    @GetMapping("/getAll")
    public ResponseData<Object> getAddress(@RequestParam(value = "stuid", required = false) Integer stuid,
                                           @RequestParam(value = "status", required = false) String status,
                                           Address address) {
        // 调用service层方法，并传递stuid和status参数
        return returnObject(addressService.getAllAddresses(stuid, status));
    }

    //修改地址
    @PutMapping("/updateAll")
    public ResponseData<Boolean> update(@RequestBody Address address){
        return returnBoolean(addressService.update(address));
    }
    //根据id删除地址
    @DeleteMapping("/del/{id}")
    public ResponseData<Boolean> del(@PathVariable("id") Integer id){
        return returnBoolean(addressService.del(id));
    }

    //添加地址
    @PostMapping("/add")
    public ResponseData<Boolean> add(@RequestBody Address address){
        System.out.println("添加地址"+address);
        return returnBoolean(addressService.add(address));

    }


    // 设置默认地址的接口
    @PostMapping("/setDefaultAddress/{id}")
    public ResponseEntity<?> setDefaultAddress(@PathVariable("id") Integer id) {
        try {
            // 调用服务层方法设置默认地址
            addressService.setDefaultAddress(id);
            // 如果成功，返回200 OK状态码和相应的消息或数据
            return ResponseEntity.ok().body("Default address set successfully.");
        } catch (Exception e) {
            // 如果发生异常，返回400 Bad Request状态码和错误消息
            return ResponseEntity.badRequest().body("Error setting default address: " + e.getMessage());
        }
    }
}
